define(['app', 'pagination', 'datetime', 'autocomplete', 'editor', 'filesaver', 'fileupload'], function(app) {
    'use strict';
    app.controller('QyService', function($scope, dataFactoryCRM, $q, Upload, $http, $rootScope) {
        $scope.qyservicePage = { pageAll: 1, pageNow: 1, pageSize: $scope.psize, k: 3 };    //分页参数
        $scope.search_param = { name: '',type: -1};   //页面搜索参数
        $scope.service_list;
        $scope.search_serviceTypes = [{ k: '-1', v: '全部' }, { k: '1', v: '内容' }, { k: '2', v: '功能' }];
        $scope.serviceTypes = [{ k: '1', v: '内容' }, { k: '2', v: '功能' }];
        /**获取列表页 */
        $scope.getQyServiceList = function() {
            var listParam = $scope.search_param;
            listParam['psize'] = $scope.qyservicePage.pageSize;
            listParam['p'] = $scope.qyservicePage.pageNow;
            var search_url = 'QyService/index?' + $.param(listParam);
            dataFactoryCRM.getDataByAPI(search_url).then(function(returnDate) {
                $scope.rcode = returnDate.rcode;
                $scope.qyservicePage.pageSize = returnDate.psize;
                $scope.qyservicePage.pageAll = returnDate.pall;
                $scope.qyservicePage.pageNow = returnDate.p;
                $scope.service_list = returnDate.data;
            });
        };
        /**搜索事件**/
        $scope.search = function() {
            $scope.getQyServiceList();
        }

        /*成功弹窗弹出事件*/
        $scope.successFun = function(){
            setTimeout(function() {
                $('.ui.modal.OK').modal('show');
                setTimeout(function() {
                    $('.ui.modal.OK').modal('hide')
                }, 3000)
            }, 1000);
        }
        /*失败弹窗弹出事件*/
        $scope.failFun = function(){
            $(".ui.modal.Fail").modal('show');
            setTimeout(function() {
                $('.ui.modal.Fail').modal('hide')
            }, 3000);
        }

        /*添加服务界面*/
        $scope.showAddService = function(){
            $(".frm.add").form('reset');
            $('.add_service.modal').modal({
                observeChanges: true,
                onApprove: function() {
                    if ($('.frm.add').form('is valid')) {
                        $scope.addService($('.frm.add').serialize());
                    } else   return false;
                }
            }).modal('show');
        }

        /*添加服务功能*/
        $scope.addService = function(addUrl){
            dataFactoryCRM.postDataByAPI('QyService/addService', addUrl)
                .then(function(data) {
                    if (data.rcode) {
                        $scope.msg = '添加服务成功';
                        $scope.successFun();
                        $scope.getQyServiceList();
                        return true;
                    } else {
                        $scope.msg = '添加服务失败';
                        $scope.failFun();
                        return false;
                    }
                });
        }

        /*编辑服务界面*/
        $scope.showEditService = function(){
            $(".frm.edit").form('reset');
            var _vservice = this.vservice;
            $('.frm.edit input[name="edit_name"]').val(_vservice.name);
            $('.frm.edit textarea[name="edit_remark"]').val(_vservice.remark);
            $("#edit_type").dropdown('set selected', _vservice.type);
            $("#update_service_id").val(_vservice.service_id);
            $('.edit_service.modal').modal({
                observeChanges: true,
                onApprove: function() {
                    if ($('.frm.edit').form('is valid')) {
                        $scope.editService($('.frm.edit').serialize());
                    } else   return false;
                }
            }).modal('show');
        }
        /*编辑服务功能*/
        $scope.editService = function(editUrl){
            dataFactoryCRM.postDataByAPI('QyService/editService', editUrl)
                .then(function(data) {
                    if (data.rcode && data.rcode==1 || data.rcode==2 ) {
                        $scope.msg = '更新服务成功';
                        $scope.successFun();
                        $scope.getQyServiceList();
                        return true;
                    } else if(!data.rcode){
                        $scope.msg = '更新服务失败';
                        $scope.failFun();
                        return false;
                    }
                });
        }

        /*删除服务*/
        $scope.delService = function(){
            var _vservice = this.vservice;
            $('.del.modal').modal({
                onApprove: function() {
                    var delUrl = 'QyService/delService?service_id=' + _vservice.service_id;
                    dataFactoryCRM.getDataByAPI(delUrl)
                        .then(function(data) {
                            if (data.rcode == 1) {
                                $scope.msg = '服务删除成功';
                                $scope.successFun();
                                $scope.getQyServiceList();
                            } else if (data.rcode == 2) {
                                $scope.msg = data.msg;
                                $scope.failFun();
                                return false;
                            } else {
                                $scope.msg = '服务删除失败';
                                $scope.failFun();
                                return false;
                            }
                        });
                    return true;
                }
            }).modal('show'); 
        }



        //初始化页面显示数据（如果是新弹出独立窗口则从父窗口获取）
        window.loadClientCacheToScope($scope, function(){
           $scope.getQyServiceList();
        });

    });
})